package net.example.model.contract;

import com.oraycn.omcs.utils.BufferUtils;
import com.oraycn.omcs.utils.SerializeUtils;

import net.example.utils.SerializeHelper;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import io.netty.buffer.ByteBuf;

public class HandleAddFriendRequestContract
{
    public String RequesterID ;

    public String AccepterID ;

    public String AccepterCatalogName ;

    public String RequesterCatalogName;

    public boolean IsAgreed ;

    public HandleAddFriendRequestContract(){}

    public HandleAddFriendRequestContract(String requesterID, String accepterID, String accepterCatalogName, boolean agree)
    {
        this.RequesterID = requesterID;
        this.AccepterID = accepterID;
        this.AccepterCatalogName = accepterCatalogName;
        this.IsAgreed = agree;
        this.RequesterCatalogName="";
    }

    public byte[] serialize() throws Exception {
        byte[] bAccepterID = AccepterID.getBytes(StandardCharsets.UTF_8);
        byte[] bAccepterCatalogName = AccepterCatalogName.getBytes(StandardCharsets.UTF_8);
        byte[] bRequesterID = RequesterID.getBytes(StandardCharsets.UTF_8);
        byte[] bRequesterCatalogName=RequesterCatalogName.getBytes(StandardCharsets.UTF_8);
        int contractLen = 4 + 4 +bAccepterCatalogName.length+4 +bAccepterID.length+1+4+bRequesterCatalogName.length+4+bRequesterID.length;
        ByteBuf body = BufferUtils.newBuffer();
        body.writeInt(contractLen);
        body.writeInt(bAccepterCatalogName.length);
        body.writeBytes(bAccepterCatalogName);
        body.writeInt(bAccepterID.length);
        body.writeBytes(bAccepterID);
        body.writeBoolean(this.IsAgreed);
        body.writeInt(bRequesterCatalogName.length);
        body.writeBytes(bRequesterCatalogName);
        body.writeInt(bRequesterID.length);
        body.writeBytes(bRequesterID);
        byte[] result = new byte[body.readableBytes()];
        System.arraycopy(body.array(),0,result,0,result.length);
        return result;
    }

    public void deserialize(byte[] info) throws IOException
    {
        ByteBuf buffer = SerializeHelper.wrappedBuffer(info);
        buffer.readInt();
        this.AccepterCatalogName = SerializeUtils.readStrIntLen(buffer);
        this.AccepterID = SerializeUtils.readStrIntLen(buffer);
        this.IsAgreed = buffer.readBoolean();
        this.RequesterCatalogName=SerializeUtils.readStrIntLen(buffer);
        this.RequesterID = SerializeUtils.readStrIntLen(buffer);
    }
}

